package com.imau.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener
public class SessionListener implements HttpSessionListener, HttpSessionAttributeListener {

    //监听在线人数
    public static int countLine = 0;

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        countLine++;
        //存储到整个应用域中
        se.getSession().getServletContext().setAttribute("countLine",countLine);
        System.out.println("当前在线人数：" + countLine);

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        countLine--;
        //存储到整个应用域中
        se.getSession().getServletContext().setAttribute("countLine",countLine);
        System.out.println("当前在线人数：" + countLine);
        System.out.println("sessionDestroyed...");
    }


    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        System.out.println("attributeAdded...");
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
        System.out.println("attributeRemoved...");
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {
        System.out.println("attributeReplaced...");
    }
}
